import Vue from "vue";
import {request} from "../../../util/request"
import "../../../common/common.less";
import "./edit.less";
import dialog from "../../../util/dialog.js";
import E from "wangeditor";
new Vue({
    el:"#louie",
    data:{
        url:document.querySelector("#louie").getAttribute("data-url"),
        goods_id:document.querySelector("#louie").getAttribute("data-goods-id"),  //商品id
        setting:{},  //系统配置
        detail_editor:null,  //商品详情编辑器
        name:"",  //商品名称
        price:"",  //商品价格
        weight:1,  //商品排序权重
        inventory:0,  //库存
        sales:0,  //销量
        images:[],   //商品图片
        detail:"",  //商品详情
        status:2   //状态 1=正常上架  2=下架中
    },
    created:function(){
        if(this.goods_id !== '0'){
            this.getGoods();
        }
        this.getSetting();
    },
    mounted:function(){
        this.createEditor();
    },
    methods:{
        //Run
        createEditor(){
            this.detail_editor = new E(this.$refs.detail);
            this.detail_editor.customConfig.uploadImgServer = this.url;
            this.detail_editor.customConfig.uploadImgParams = {
                pages:'file',
                doing:'uploadEditorImage'
            }
            this.detail_editor.customConfig.zIndex = 2;
            this.detail_editor.customConfig.onchange = (html)=>{
                this.detail = html;
            }
            this.detail_editor.create();
        },
        /**
         * 获取商品数据
         */
        getGoods(){
            request("goods/getGoods",{
                goods_id:this.goods_id
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        let data = res.data.data;
                        this.name = data.name;
                        this.price = data.price;
                        this.weight = data.weight;
                        this.inventory = data.inventory;
                        this.sales = data.sales;
                        this.detail = data.detail;
                        this.status = data.status;
                        this.detail_editor.txt.html(data.detail);
                    }
                });
        },
        /**
         * 获取系统的货币名称
         */
        getSetting(){
            request("setting/easyFind",{
                return:["currency_name"]
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.setting = res.data.data;
                    }
                });
        },

        //Handle
        /**
         *提交
         */
        submit(){
            this.getGoodsImages();
            if(!this.checkSubmit()) return false;
            let data = {
                name:this.name,
                images:this.images,
                price:this.price,
                weight:this.weight,
                inventory:this.inventory,
                sales:this.sales,
                detail:this.detail,
                status:this.status
            }
            let doing = this.goods_id === '0' ? 'add' : 'edit';
            request("goods/"+doing,{
                goods_id:this.goods_id,
                data
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        if(this.goods_id ==='0') this.goods_id = res.data.data;
                        dialog.alert(this,"操作成功");
                    }else{
                        dialog.alert(this,"操作失败");
                    }
                });
        },
        /**
         * 获取商品图片
         */
        getGoodsImages(){
            this.images = [];
            let images_input = this.$refs.images.querySelectorAll("input[name='images[]']");
            images_input.forEach((item)=>{
                this.images.push(item.value);
            });
        },
        /**
         * 提交验证信息
         */
        checkSubmit(){
            if(this.name === ""){
                dialog.alert(this,"请输入商品名称");
                return false;
            }
            if(this.name.length > 50){
                dialog.alert(this,"商品名称长度不能大于50");
                return false;
            }
            if(parseInt(this.price) <= 0 || !/^\d+$/.test(this.price)){
                dialog.alert(this,"请输入正确的兑换价格");
                return false;
            }
            if(!/^\d+$/.test(this.weight)){
                dialog.alert(this,"请输入正确的权重");
                return false;
            }
            if(!/^\d+$/.test(this.inventory)){
                dialog.alert(this,"请输入正确的库存");
                return false;
            }
            if(!/^\d+$/.test(this.sales)){
                dialog.alert(this,"请输入正确的销量");
                return false;
            }
            if(this.images.length <= 0){
                dialog.alert(this,"至少上传一张商品图片");
                return false;
            }
            if(this.detail === ""){
                dialog.alert(this,"请输入商品详情");
                return false;
            }
            return true;
        }
    }
});